package com.ruoyi.business.controller;

import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.service.*;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.common.riskRule.RuleStrategy;
import com.ruoyi.business.component.MqProduce;
import com.ruoyi.business.mapper.BusinessMapper;
import com.ruoyi.business.service.ICreditService;
import com.ruoyi.business.service.ILoanApplyService;
import com.ruoyi.business.service.IPayTransService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @Author: cyh
 * @Date: 2021/6/10 11:08 上午
 */
@Slf4j
@RestController
@RequestMapping("/retry")
public class RetryController {


    @Autowired
    ICreditService creditService;

    @Autowired
    ILoanApplyService loanService;

    @Autowired
    ILoanOrderService loanOrderService;

    @Autowired
    IPayTransService payTransService;

    @Autowired
    ICustBvnMatchService custBvnMatchService;

    @Autowired
    BusinessMapper businessMapper;

    @Autowired
    protected ICreditApplyService creditApplyService;

    @Autowired
    protected ICreditFlagService creditFlagService;

    @Autowired
    protected MqProduce producer;

    /**
     * 手动批量授信
     */
    @GetMapping("test")
    public String manualBatchCredit() {
        return "请求成功";
    }
    /**
     * 手动批量授信
     */
    @PostMapping("credit/{applyNos}")
    @Log(title = "手动批量授信", businessType = BusinessType.INSERT)
    public Object manualBatchCredit(@PathVariable String[] applyNos) {

        Set<String> collect = new HashSet<>(Arrays.asList(applyNos));
        HashMap<Object, Object> resMap = new HashMap<>();

        List<String> accessRes = new ArrayList<>();
        List<Map> errorRes = new ArrayList<>();
        List<String> skipRes = new ArrayList<>();
        for (String applyNo : collect) {
            try {
                //前置校验
                CreditApply creditApply = new CreditApply();
                creditApply.setApplySerialNo(applyNo);
                creditApply.setApplyStatus(BizConstants.APPLY_STATUS_9_APPROVALING);
                List<CreditApply> creditApplies = creditApplyService.selectCreditApplyList(creditApply);
                if (creditApplies.size() == 0) {
                    throw new BusinessException(RspEnmu.SERIAL_NOT_EXIST);
                }
                creditApply = creditApplies.get(0);
                if (BizConstants.APPLY_STATUS_9_APPROVALING != creditApply.getApplyStatus()) {
                    throw new BusinessException(RspEnmu.LOAN_STATUS_IS_NOT_APPROVAL);
                }

                CreditFlag creditFlagQuery = new CreditFlag();
                creditFlagQuery.setSerialNo(applyNo);
                creditFlagQuery.setType(RuleStrategy.CREDIT);
                List<CreditFlag> creditFlags = creditFlagService.selectCreditFlagList(creditFlagQuery);
                if (creditFlags.size() == 0 || Func.hasEmpty(creditFlags.get(0).getOrderFlag(), creditFlags.get(0).getDeviceFlag(), creditFlags.get(0).getAppsFlag(), creditFlags.get(0).getContactsFlag())) {
                    creditApply.setApplyStatus(BizConstants.APPLY_STATUS_10_CANCEL);
                    creditApply.setUpdateTime(new Date());
                    creditApplyService.updateCreditApply(creditApply);
                    skipRes.add(applyNo);
                    continue;
                }
                creditService.rulePass(applyNo);
                accessRes.add(applyNo);
            } catch (Exception e) {
                log.error("manualBatchCredit 异常：", e);
                HashMap<Object, Object> bad = new HashMap<>();
                bad.put(applyNo, e.getMessage());
                errorRes.add(bad);
            }
        }
        resMap.put("accessRes", accessRes);
        resMap.put("badRes", errorRes);
        resMap.put("skipRes", skipRes);
        return AjaxResult.success(resMap);
    }


}
